﻿Imports ccapps.persistent.LACustomerDetails
Public Class LACustomerDetails

    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "
    Protected WithEvents ContentPaneFooter2 As Library.WebControls.ContentPaneFooter
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub


    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()

    End Sub

#End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.PageTitle = "LA Müşteri Detayları"

        Me.ContentPaneFooter2.ConfirmButton.Visible = False
        SetDefaultButton(Me, Me.txtCustCode, Me.btnAuthenticateCustomer)
        SetDefaultButton(Me, Me.txtMSISDN, Me.btnAuthenticateCustomer)

        If Not Request.QueryString("CustCode") Is Nothing Then
            Me.txtCustCode.Text = Request.QueryString("CustCode")
            Me.btnAuthenticateCustomer_Click(Me, New EventArgs)
        End If
    End Sub



    Private Sub getCustomerDetailFromCX()
        Dim pin_customer_id As Integer

        If Me.txtCustCode.Text.Trim().Length > 0 Then
            CustCode = Me.txtCustCode.Text

            Dim dt2 As DataTable = GetCustInfoFromCustcode(CustCode)


            If dt2.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt2.Rows


                    pin_customer_id = Convert.ToInt32((dr2("CUSTOMER_ID").ToString()))
                    'Label4.Text = pin_customer_id.ToString()


                Next

            End If



        ElseIf Me.txtMSISDN.Text.Trim().Length > 0 Then

            Dim dt3 As DataTable = GetCustInfoFromMSISDN(txtMSISDN.Text.Trim())


            If dt3.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt3.Rows


                    pin_customer_id = dr2("CUSTOMER_ID").ToString()


                Next

            End If
            'Label4.Text = dt3.Rows(0)("CUSTOMER_ID").ToString()

        End If


        Dim dt As DataTable = GetCustomerContactDetails(pin_customer_id)
        'Label4.Text = dt.Rows.Count



        If dt.Rows.Count > 0 Then
            For Each dr2 As DataRow In dt.Rows




                If (dr2("INDIVIDUAL_OVERDISC_FLAG").ToString() = "X") Then
                    chckDeactiveOverdiscounting.Checked = True
                Else
                    chckDeactiveOverdiscounting.Checked = False
                End If

                If (dr2("SUPPRESS_INV_EXEMPTION_FLAG").ToString() = "X") Then
                    chckLimitAlti.Checked = True
                Else
                    chckLimitAlti.Checked = False
                End If


                'lblCustName.Text = dr2("CCNAME").ToString()
                'lblCustFname.Text = dr2("CCFNAME").ToString()
                'lblCustLname.Text = dr2("CCLNAME").ToString()
                'lblStreet.Text = dr2("ccstreet").ToString()
                'lblStreetNo.Text = dr2("ccstreetno").ToString()
                'lblADR1.Text = dr2("ccaddr1").ToString()
                'lblADR2.Text = dr2("ccaddr2").ToString()
                'lblADR3.Text = dr2("ccaddr3").ToString()
                'lblZIP.Text = dr2("CCZIP").ToString()
                'lblCity.Text = dr2("CCCITY").ToString()
                'lblContactADR1.Text = dr2("cctn_area").ToString()
                'lblContact1.Text = dr2("cctn").ToString()
                'lblContactADR2.Text = dr2("cctn2_area").ToString()
                'lblContact2.Text = dr2("cctn2").ToString()
                lblFaxADR.Text = dr2("CCFAX_AREA").ToString()
                lblFax.Text = dr2("CCFAX").ToString()
                lblAuthorizedPersonsEmail.Text = dr2("CCEMAIL").ToString()
                lblSMSNo.Text = dr2("CCSMSNO").ToString()
                lblDriverLicence.Text = dr2("CSDRIVELICENCE").ToString()
                lblLanguage.Text = dr2("LanguageCode").ToString()
                lblNationality.Text = dr2("nationality").ToString()
                lblCustomerType.Text = dr2("CSType").ToString()
                'lblDataLevel.Text = dr2("CS_DATA_LEVEL").ToString()
                lblAuthorizedPersonsFatherName.Text = dr2("csremark_1").ToString()
                lblMotherName.Text = pin_customer_id.ToString()

                lblMotherMaidenName.Text = dr2("csremark_2").ToString()
                lblRegistiredProvince.Text = dr2("REGISTERED_PROVINCE").ToString()
                lblRegistiredDistrict.Text = dr2("REGISTERED_DISTRICT").ToString()
                lblBirhPlaceProvince.Text = dr2("BIRTH_PLACE_PROVINCE").ToString()
                lblBirthPlaceDistrict.Text = dr2("BIRTH_PLACE_DISTRICT").ToString()

                If (dr2("CS_DATA_SHARE").ToString() = "X") Then
                    chckDataShare.Checked = True
                Else
                    chckDataShare.Checked = False
                End If
                lblEmployer.Text = dr2("CSEMPLOYER").ToString()
                If (dr2("EMPLOYEE").ToString() = "X") Then
                    chckEmployeeOfYourCompany.Checked = True
                Else
                    chckEmployeeOfYourCompany.Checked = False
                End If




            Next

        End If

    End Sub
    Private Function getDealer(ByVal custCode As String) As Dealer


        Dim dtCustomersHaveContracts As DataTable = getDealer1(custCode)


        If dtCustomersHaveContracts.Rows.Count > 0 Then

            Dim dtDealerIDs As DataTable = getDealer2(custCode)
            If dtDealerIDs.Rows.Count > 0 Then
                Return New Dealer(dtDealerIDs.Rows(0)("DEALER_ID"), dtDealerIDs.Rows(0)("DEALER_NAME"))
            Else
                Return New Dealer(Nothing, Nothing)
            End If

        Else
            Return New Dealer(Nothing, Nothing)

        End If
    End Function



    Private Function getSecondInfo(ByVal custCode As String) As DataTable

    End Function

    Private Sub btnAuthenticateCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAuthenticateCustomer.Click
        'Dim dt As DataTable = persistent.myDBConn.myORATable("select  * from storage_medium where sm_serialnum = (select sm_id from port where port_num = 286035710173159)")
        'Response.Write(dt.Rows(0)("SM_PUK"))
        Dim dtcustcontrol As DataTable = New DataTable()

        Me.Lblok.Text = ""
        Dim custCode As Object = Me.txtCustCode.Text
        getCustomerDetailFromCX()
        If Me.txtCustCode.Text.Trim().Length > 0 Then
            dtcustcontrol = CustControl(txtCustCode.Text)

            custCode = Me.txtCustCode.Text
        ElseIf Me.txtMSISDN.Text.Trim().Length > 0 Then
            Dim dt As DataTable = getCustCode(txtMSISDN.Text.Trim())
            custCode = dt.Rows(0)(0).ToString()
            If Me.chkGetPaymentResponsible.Checked Then
                custCode = persistent.RetantionActivate.getCustCode(CInt(persistent.RetantionActivate.getPaymentResponsibleCustomerId(persistent.RetantionActivate.getCustomerId(custCode))))

                'Response.Write(dtcustcontrol.Rows.Count)
            End If
            dtcustcontrol = CustControl(custCode)

        Else
            Me.Lblok.Text = "Custcode veya MSISDN girmelisiniz!"
            Return
        End If
        'ElseIf Not (CInt(dtCustomer.Rows(0)("cslevel").ToString()) = 10 OrElse CInt(dtCustomer.Rows(0)("cslevel").ToString()) = 20 OrElse CInt(dtCustomer.Rows(0)("cslevel").ToString()) = 30) Then
        Try
            If Not dtcustcontrol.Rows(0)(0).ToString = "CORP" Then
                Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Sadece LA Customer ile İşlem Yapabilirsiniz.');</script>")
                'Label4.Text = dtcustcontrol.Rows.Count.ToString()


            Else
                Dim dtCustomerDetail As DataTable = getCustomerDetail(custCode)
                '   Label3.Text = dtCustomerDetail.Rows.Count.ToString()
                If dtCustomerDetail Is Nothing OrElse dtCustomerDetail.Rows.Count = 0 Then
                    Me.tblContactInfo.Visible = False
                    Me.dgContracts.DataSource = Nothing
                    Me.dgContracts.DataBind()
                    Me.dgPromotions.DataSource = Nothing
                    Me.dgPromotions.DataBind()
                    Me.dgfu.DataSource = Nothing
                    Me.dgfu.DataBind()
                    Me.dgFatura.DataSource = Nothing
                    Me.dgFatura.DataBind()
                    Me.dgPaymentResponsibles.DataSource = Nothing
                    Me.dgPaymentResponsibles.DataBind()
                    Me.dgSpecificPacks.DataSource = Nothing
                    Me.dgSpecificPacks.DataBind()
                    'Me.lblSpecialPacks.Visible = False
                    Me.tblReport.Visible = False
                    Me.Lblok.Text = "Abone bulunamadı!"
                    Return
                Else
                    Me.CustCode = custCode
                    Me.tblContactInfo.Visible = True
                    Dim value As Object
                    For Each column As DataColumn In dtCustomerDetail.Columns
                        Dim label As Control = Me.FindControl("lbl" & column.ColumnName)
                        If Not label Is Nothing Then
                            value = dtCustomerDetail.Rows(0)(column.ColumnName)
                            If (Not IsDBNull(value)) Then
                                If IsNumeric(value) AndAlso value.ToString().Length < 19 AndAlso CDbl(value) - CLng(value) <> 0 Then
                                    value = CDbl(value).ToString("N2")
                                End If
                                DirectCast(label, Label).Text = value
                            Else
                                DirectCast(label, Label).Text = "-"
                            End If
                            'DirectCast(label, Label).Text = Pick(Not IsDBNull(dtCustomerDetail.Rows(0)(column.ColumnName)), dtCustomerDetail.Rows(0)(column.ColumnName), "-") 

                        End If
                    Next
                    lblMotherName.Text = dtCustomerDetail.Rows(0)("MOTHERS_NAME").ToString()
                    Dim dealer As Dealer = Me.getDealer(Me.CustCode)
                    Me.lblDealerID.Text = dealer.DealerID
                    Me.lblDealerName.Text = dealer.DealerName
                    Me.bindContractsList()
                    Me.bindPromotionsList()
                    Me.bindFuList()
                    Me.bindFaturaList()
                    Me.bindPenaltyList()
                    Me.bindPaymentResponsibles()
                    Me.bindSpecificPacks()
                    'Me.lblSpecialPacks.Visible = True
                    Me.tblReport.Visible = True

                    Me.logCustomerDetailView()


                End If
            End If
        Catch ex As Exception
            Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Abone Bulunamadı.');</script>")
        End Try
       

        'If custCode Is Nothing OrElse IsDBNull(custCode) OrElse custCode.Trim().Length = 0 Then
        '    Me.Lblok.Text = "Abone bulunamadı!"
        '    Return
        'End If
      

       
    End Sub

    Private Function logCustomerDetailView() As Boolean
        Dim query As String = String.Format("insert into eu_bill.customer_details_view_log (USERID, CUSTCODE, VIEWDATE) values ('{0}', '{1}', sysdate)", _
        Session("USER"), Me.CustCode)
        Return persistent.myDBConn.ORAExecute(query) = 1
    End Function

    Private Sub dgContracts_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgContracts.PageIndexChanged
        Me.bindContractsList(e.NewPageIndex)
    End Sub

    Private Sub dgPromotions_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgPromotions.PageIndexChanged
        Me.bindPromotionsList(e.NewPageIndex)
    End Sub
    Private Sub dgFu_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgfu.PageIndexChanged
        Me.bindFuList(e.NewPageIndex)
    End Sub

    Private Sub dgFatura_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgFatura.PageIndexChanged
        Me.bindFaturaList(e.NewPageIndex)
    End Sub

    Private Sub dgPenalty_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgPenalty.PageIndexChanged
        Me.bindPenaltyList(e.NewPageIndex)
    End Sub

    Private Sub bindContractsList(Optional ByVal pageIndex As Integer = 0)
        Me.dgContracts.CurrentPageIndex = pageIndex
        Me.dgContracts.DataSource = getContractsDetail(Me.CustCode)
        Me.dgContracts.DataBind()

        Me.dtlOtherAuthorizedPersons.DataSource = persistent.myDBConn.myORATable _
        ( _
            String.Format _
            ( _
            "select nvl(ccfname, '') || ' ' || nvl(cclname, '') OTHER_AUTHORIZED_PERSON from ccontact_all where customer_id = (select customer_id from customer_All where custcode = '{0}') and ccbill is null", Me.CustCode _
            ) _
        )

        Me.dtlOtherAuthorizedPersons.DataBind()
    End Sub

    Private Sub bindPromotionsList(Optional ByVal pageIndex As Integer = 0)
        Me.dgPromotions.CurrentPageIndex = pageIndex
        Me.dgPromotions.DataSource = getPromotions(Me.CustCode)
        Me.dgPromotions.DataBind()
    End Sub

    Private Sub bindFuList(Optional ByVal pageIndex As Integer = 0)
        Me.dgfu.CurrentPageIndex = pageIndex
        Me.dgfu.DataSource = getFu(Me.CustCode)
        Me.dgfu.DataBind()
    End Sub

    Private Sub bindFaturaList(Optional ByVal pageIndex As Integer = 0)
        Me.dgFatura.CurrentPageIndex = pageIndex
        Me.dgFatura.DataSource = getFatura(Me.CustCode)
        Me.dgFatura.DataBind()
    End Sub

    Private Sub bindPenaltyList(Optional ByVal pageIndex As Integer = 0)
        Me.dgPenalty.CurrentPageIndex = pageIndex
        Me.dgPenalty.DataSource = getPenalty(Me.CustCode)
        Me.dgPenalty.DataBind()
    End Sub

    Private Sub bindPaymentResponsibles(Optional ByVal pageIndex As Integer = 0)
        Me.dgPaymentResponsibles.CurrentPageIndex = pageIndex
        Me.dgPaymentResponsibles.DataSource = getPaymentResponsibleCustCodes(Me.CustCode)
        Me.dgPaymentResponsibles.DataBind()
    End Sub

    Private Sub bindSpecificPacks(Optional ByVal pageIndex As Integer = 0)
        Me.dgSpecificPacks.CurrentPageIndex = pageIndex
        Me.dgSpecificPacks.DataSource = getCallSMSBlackberryPacks(Me.CustCode)
        Me.dgSpecificPacks.DataBind()
    End Sub

    Private Sub lbtnExportToExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnExportToExcel.Click

        Dim currentPageIndexes As New System.Collections.Specialized.NameValueCollection

        currentPageIndexes.Add("dgPromotions", dgPromotions.CurrentPageIndex)
        currentPageIndexes.Add("dgfu", dgfu.CurrentPageIndex)
        currentPageIndexes.Add("dgFatura", dgFatura.CurrentPageIndex)
        currentPageIndexes.Add("dgPenalty", dgPenalty.CurrentPageIndex)
        currentPageIndexes.Add("dgContracts", dgSpecificPacks.CurrentPageIndex)

        Dim stringWrite As New System.IO.StringWriter

        Dim htmlWrite As New HtmlTextWriter(stringWrite)


        If clstPiecesToReport.Items(0).Selected Then
            Me.tblContactInfo.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(1).Selected Then
            Me.dgPromotions.AllowPaging = False
            Me.bindPromotionsList()
            Me.dgPromotions.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(2).Selected Then
            Me.dgfu.AllowPaging = False
            Me.bindFuList()
            Me.dgfu.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(3).Selected Then
            Me.dgFatura.AllowPaging = False
            Me.bindFaturaList()
            Me.dgFatura.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(4).Selected Then
            Me.dgPenalty.AllowPaging = False
            Me.bindPenaltyList()
            Me.dgPenalty.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(5).Selected Then
            Me.dgContracts.AllowPaging = False
            Me.bindContractsList()
            Me.dgContracts.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(6).Selected Then
            'Me.bindPaymentResponsibles()
            Me.dgPaymentResponsibles.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(7).Selected Then
            'Me.bindSpecificPacks()
            Me.dgSpecificPacks.RenderControl(htmlWrite)
        End If

        'dgContracts()
        'dgPromotions()
        'dgfu()
        'dgFatura()
        'dgPenalty()
        'dgPaymentResponsibles()
        'dgSpecificPacks()

        'Me.bindContractsList()
        'Me.bindPromotionsList()
        'Me.bindFuList()
        'Me.bindFaturaList()
        'Me.bindPenaltyList()
        'Me.bindPaymentResponsibles()
        'Me.bindSpecificPacks()


        Me.setFiletoDownload("Requests.xls", stringWrite.ToString())


        If clstPiecesToReport.Items(1).Selected Then
            Me.dgPromotions.AllowPaging = True
            Me.bindPromotionsList(currentPageIndexes("dgPromotions"))
            Me.dgPromotions.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(2).Selected Then
            Me.dgfu.AllowPaging = True
            Me.bindFuList(currentPageIndexes("dgfu"))
            Me.dgfu.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(3).Selected Then
            Me.dgFatura.AllowPaging = True
            Me.bindFaturaList(currentPageIndexes("dgFatura"))
            Me.dgFatura.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(4).Selected Then
            Me.dgPenalty.AllowPaging = True
            Me.bindPenaltyList(currentPageIndexes("dgPenalty"))
            Me.dgPenalty.RenderControl(htmlWrite)
        End If

        If clstPiecesToReport.Items(5).Selected Then
            Me.dgContracts.AllowPaging = True
            Me.bindContractsList(currentPageIndexes("dgContracts"))
            Me.dgContracts.RenderControl(htmlWrite)
        End If

        'Me.listHistory(currentPageIndex)

    End Sub

    Private Sub setFiletoDownload(ByVal fileName As String, ByVal fileContent As String)

        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=" & fileName)

        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        Response.ContentType = "application/vnd.ms-excel"

        Response.Charset = ""

        Me.EnableViewState = False

        Response.Write(fileContent)

        Response.End()

    End Sub

    Private Property CustCode() As String
        Get
            Return ViewState("__CustCode")
        End Get
        Set(ByVal Value As String)
            ViewState("__CustCode") = Value
        End Set
    End Property

    Private Class Dealer
        Private _DealerID As String = "-", _DealerName As String = "-"

        Public Sub New(ByVal dealerID As Object, ByVal dealerName As Object)
            If Not (dealerID Is Nothing OrElse IsDBNull(dealerID)) Then
                Me._DealerID = dealerID.ToString()
            End If

            If Not (dealerName Is Nothing OrElse IsDBNull(dealerName)) Then
                Me._DealerName = dealerName.ToString()
            End If

        End Sub

        Public ReadOnly Property DealerID() As String
            Get
                Return Me._DealerID
            End Get
        End Property

        Public ReadOnly Property DealerName() As String
            Get
                Return Me._DealerName
            End Get
        End Property

    End Class


End Class